import requests
import re
import time
from urllib.parse import  urlparse,urlencode,urljoin,quote
import execjs

# @todo    python 执行 JavaScript 文件
# @param   file      js 文件路径
# @param   function  要执行的js方法
# @param   *param    传入js的参数个数不定

# https://cache.video.iqiyi.com/dash?
# tvid=8169758347687500
# &bid=300
# &vid=6a5a0633528b7d08798abc5b336efb3e
# &src=01010031010000000000
# &vt=0
# &rs=1&uid=1273857213
# &ori=pcw
# &ps=0
# &k_uid=034fe22701305e2de67949fd31567b37
# &pt=0&d=0&s=&lid=&cf=&ct=
# &authKey=df64164e7ebfff805be914e6ab489ecd
# &k_tag=1
# &ost=0
# &ppt=0
# &dfp=a05fe2a395a2fa46eca6371e95159ebaa872b99e0b162f0f78167323997c30c685
# &locale=zh_cn
# &prio=%7B%22ff%22%3A%22f4v%22%2C%22code%22%3A2%7D
# &pck=24XcOhYGapom3Pa5KBgTem3F2F6QMcwm36hYm3IyHg5FgjKxsoIm257b0ln1Sm3m2WU94bBgu58
# &k_err_retries=0
# &up=
# &qd_v=2
# &tm=1636722365045
# &qdy=a
# &qds=0
# &k_ft1=706436220846084
# &k_ft4=1162183859249156
# &k_ft5=262145
# &bop=%7B%22version%22%3A%2210.0%22%2C%22dfp%22%3A%22a05fe2a395a2fa46eca6371e95159ebaa872b99e0b162f0f78167323997c30c685%22%7D
# &ut=0&vf=9b10f4fec7b3aa1cab765003d31f1234


def execJs(file,function,*param):
    f = open(file, 'r', encoding='UTF-8')
    js = execjs.compile(f.read())
    f.close()
    return js.call(function,*param)



def getpre(url):
    headers = {
        "userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36"

    }
    r = requests.get(url, headers=headers)
    r.raise_for_status()
    res = r.text
    # print(res)
    # s=re.search("var param = \{\};([\s\S]*) param\[\'isSupportAI",res).group()
    # print(res)
    tvid = re.search('param\[\'tvid\'\] = \"(\d*)\"', res).groups()[0]
    vid = re.search('param\[\'vid\'\] = \"([a-z0-9]*)\"', res).groups()[0]
    return {'tvid':tvid,'vid':vid}






def getm3u8(url):
    pre =getpre(url)
    param = {}
    tm              = str(time.time()).replace('.','')[0:13]

    param['tvid']   = pre['tvid']
    param['bid']    = "600"                     #清晰度
    param['vid']    = pre['vid']
    param['src']    = "01010031010000000000"    #const
    param['vt']     = "0"                       #const
    param['rs']     = "1"                       #const
    param['uid']    = "1273857213"              #cookie p0003
    param['ori']    = "pcw"                     #const
    param['ps']     = "0"                       #58721
    param['k_uid']  = "034fe22701305e2de67949fd31567b37"#cookie QC005
    param['pt']     = "0"                       #const
    param['d']      = "0"                       #const
    param['s']      = ""                        #const
    param['lid']    = ""                        #const
    param['cf']     = ""                        #const
    param['ct']     = ""                        #const
    param['authKey']= execJs('zu.js','main',tm,pre['tvid'])
    param['k_tag']  = "1"
    param['ost']    = "0"
    param['ppt']    = "0"
    param['dfp']    = "a05fe2a395a2fa46eca6371e95159ebaa872b99e0b162f0f78167323997c30c685" #cookie
    param['locale'] = "zh_cn"
    param['prio']    = '{"ff":"f4v","code":2}' #const

    param['pck']    = "24XcOhYGapom3Pa5KBgTem3F2F6QMcwm36hYm3IyHg5FgjKxsoIm257b0ln1Sm3m2WU94bBgu58" #cookie p00001
    param['k_err_retries']= "0"                 #const
    param['up']     = ""
    param['qd_v']   = "2"
    param['tm']     = tm
    param['qdy']    = "a"                       #const
    param['qds']    = "0"                       #const
    param['k_ft1']  = "706436220846084"         #const
    param['k_ft4']  = "1162183859249156"        #const
    param['k_ft5']  = "262145"                  #const
    param['bop']    = '{"version":"10.0","dfp":"a05fe2a395a2fa46eca6371e95159ebaa872b99e0b162f0f78167323997c30c6851"}'
    param['ut']     = '0'
    e ="/dash?"+urljoin('',urlencode(param))
    print(e)
    vf              = execJs('test.js','main',e)
    url = "https://cache.video.iqiyi.com" +e+"&vf="+vf
    print(url)
    return url


def main():
    url='https://www.iqiyi.com/v_29mchzuik9s.html?vfrm=pcw_dianshiju&vfrmblk=F&vfrmrst=711219_dianshiju_float_pic_play2'
    url="https://www.iqiyi.com/v_19rrax9nq4.html?vfrm=pcw_playpage&vfrmblk=F&vfrmrst=80521_correlation_star_image3"
    url="https://www.iqiyi.com/v_1bsbnc4v6bo.html?vfrm=pcw_ertong&vfrmblk=B&vfrmrst=fcs_15_p1"
    url="https://www.iqiyi.com/a_1r7ca2jxii1.html?vfrm=pcw_home&vfrmblk=B&vfrmrst=fcs_0_v11"
    # url="https://www.iqiyi.com/v_19rrj68hdg.html?vfrm=pcw_my&vfrmblk=body_bf&vfrmrst=702061_bfrecord_image6"
    # url="https://www.iqiyi.com/v_1tqe6ry1t1c.html?vfrm=pcw_zongyi&vfrmblk=E&vfrmrst=711219_zongyi_qdtj_float_pic_play4"
    # url="https://www.iqiyi.com/v_188ig241tok.html?vfrm=pcw_home&vfrmblk=712211_dianshiju&vfrmrst=712211_dianshiju_image1&r_area=&r_source=&bkt=&e="
    # url="https://www.iqiyi.com/v_hf9xnzr8mc.html?vfrm=pcw_dianying&vfrmblk=E&vfrmrst=711219_dianying_float_pic_play2"
    url="https://www.iqiyi.com/v_hf9xnzr8mc.html"
    url="https://www.iqiyi.com/v_1zp7qgh23kg.html?vfrm=pcw_home&vfrmblk=CZ&vfrmrst=712211_cainizaizhui_image2&r_area=rec_you_like&r_source=230&bkt=MBA_PW_T3_93&e=ede7c2ebdf933174d402dbfe68a80dfe&stype=2"
    url = getm3u8(url)
    r   = requests.get(url)
    r.raise_for_status()
    r.encoding =r.apparent_encoding
    # dic= eval(r.text.replace('false', 'False').replace('true', 'True').replace('null','""'))
    # links = dic['data']['program']['video']
    text =r.text.replace('\\','')
    m3u8=re.search('\"#EXTM3U([\s\S]*)"ptt"',text).group(0)
    ts=re.findall('(http.*?)n#EXTINF',m3u8)
    for i in ts:
        print(i)



# main()

def load():
    url="https://open.weixin.qq.com/connect/qrconnect?appid=wx228cef5813cc7961&scope=snsapi_login&response_type=code&redirect_uri=http%3A%2F%2Fpassport.iqiyi.com%2Fapis%2Fthirdparty%2Fncallback.action%3Ffrom%3D29%26source%3D29%26agenttype%3D1%26device_id%3D361efc67038001b3a5f09bab5bfd8660%26isframe%3D1%26redirect_url%3Dhttp%253A%252F%252Fpassport.iqiyi.com%252Fapis%252Fthirdparty%252Fncallback.action%253Ffrom%253D29%26verifyPhone%3D1%26ptid%3D01010021010000000000%26exception_url%3Dhttps%253A%252F%252Fwww.iqiyi.com%252Fthirdlogin%252Fclose.html%26success_url%3Dhttps%253A%252F%252Fwww.iqiyi.com%252Fthirdlogin%252Fclose.html%26is_reg_confirm%3D1%26fromSDK%3D1&splite#wechat_redirect&state=EBB2Lj4m%252F9W4PGTzIIpVqNxUYv50jmmLTYGkBRLYNRgS0gseSFEhBFi9E8XBlbcHkqprZOCbbqsRj5ZFwdKedQ%253D%253D"
    # https://lp.open.weixin.qq.com/connect/l/qrconnect?uuid=011Dfk3c47UC000R&_=1636865355312   //轮寻地址
    # https://open.weixin.qq.com/connect/qrcode/061bhKcO30dFFa1m                                //img
    r  =requests.get(url)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    text       = r.text
    fordevtool = re.search('fordevtool = \"([\s\S]*?)\"',text).groups()[0]
    uuid       = fordevtool.split('=')[1]
    imgurl     = "https://open.weixin.qq.com/connect/qrcode/"+uuid
    print(imgurl)
    base       = str(time.time()).replace('.','')[0:12]
    url        = "https://lp.open.weixin.qq.com/connect/l/qrconnect?uuid="+uuid+'&_='+base
    count      = 1
    while(True):
        print(url+str(count))
        r=requests.get(url+str(count))
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        print(r.cookies)
        time.sleep(1)
        count +=1







load()


# http://passport.iqiyi.com/apis/thirdparty/ncallback.action?from=29&source=29&agenttype=1 &device_id=8d83eb36484a7b5064e1a06b3ae50bc2&isframe=1&redirect_url=http%3A%2F%2Fpassport.iqiyi.com%2Fapis%2Fthirdparty%2Fncallback.action%3Ffrom%3D29&verifyPhone=1&ptid=01010021010000000000&exception_url=https%3A%2F%2Fwww.iqiyi.com%2Fthirdlogin%2Fclose.html&success_url=https%3A%2F%2Fwww.iqiyi.com%2Fthirdlogin%2Fclose.html&is_reg_confirm=1&fromSDK=1&code=0414kiGa1erx7C0DJNHa1ZYAY244kiGR&state= HTTP/1.1







